import { getToken, setToken, removeToken } from '@/utils/auth'
import { getInfo, getuserImg } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userinfo: {}
  },
  mutations: {
    // 更新token
    upDataToken(state, val) {
      state.token = val
      setToken(val)
    },
    // 清除token
    delDataToken(state) {
      state.token = ''
      removeToken()
    },

    // 更新用户数据
    upDataUserInfo(state, val) {
      state.userinfo = val
    },

    // 清除用户数据
    delDataUserInfo(state) {
      state.userinfo = {}
    }
  },
  actions: {
    async loadUserInfo(context) {
      try {
        const res = await getInfo()
        console.log(res)
        // console.log(context)
        const img = await getuserImg(res.data.userId)
        context.commit('upDataUserInfo', { ...res.data, ...img.data })
      } catch (error) {
        console.log(error)
      }
    }
  }
}

